package qingma

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"google.golang.org/grpc/status"
	"qm_system_server/server/global"
	"qm_system_server/server/model/common/response"
	"qm_system_server/server/model/qingma"
	qingmaReq "qm_system_server/server/model/qingma/request"
	"qm_system_server/server/proto/grpc/operate"
)

type QmPlatformsApi struct {
}

// CreateQmPlatforms 创建qmPlatforms表
// @Tags QmPlatforms
// @Summary 创建qmPlatforms表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body qingma.QmPlatforms true "创建qmPlatforms表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /qmPlatforms/createQmPlatforms [post]
func (qmPlatformsApi *QmPlatformsApi) CreateQmPlatforms(c *gin.Context) {
	var qmPlatforms qingma.QmPlatforms
	err := c.ShouldBindJSON(&qmPlatforms)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	grpcClient := operate.NewOperateServiceClient(global.OperateGrpc)

	_, err = grpcClient.CreatePlatform(context.Background(), &operate.CreatePlatformRequest{
		Platform: &operate.Platform{
			GameHash:   qmPlatforms.GameHash,
			PlatformId: qmPlatforms.PlatformId,
			Appid:      qmPlatforms.Appid,
			Secret:     qmPlatforms.Secret,
			Cookie:     qmPlatforms.Cookie,
			Divide:     qmPlatforms.Divide,
			OpenidFlag: qmPlatforms.OpenidFlag,
			CookieTwo:  qmPlatforms.CookieTwo,
		},
	})

	if err != nil {

		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithMessage("创建成功", c)
}

// DeleteQmPlatforms 删除qmPlatforms表
// @Tags QmPlatforms
// @Summary 删除qmPlatforms表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body qingma.QmPlatforms true "删除qmPlatforms表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /qmPlatforms/deleteQmPlatforms [delete]
func (qmPlatformsApi *QmPlatformsApi) DeleteQmPlatforms(c *gin.Context) {
	ID := c.Query("ID")

	//转成int64
	id := cast.ToInt64(ID)

	grpcClient := operate.NewOperateServiceClient(global.OperateGrpc)
	_, err := grpcClient.DeletePlatform(context.Background(), &operate.DeletePlatformRequest{
		Id: id,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithMessage("删除成功", c)
}

// UpdateQmPlatforms 更新qmPlatforms表
// @Tags QmPlatforms
// @Summary 更新qmPlatforms表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body qingma.QmPlatforms true "更新qmPlatforms表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /qmPlatforms/updateQmPlatforms [put]
func (qmPlatformsApi *QmPlatformsApi) UpdateQmPlatforms(c *gin.Context) {
	var qmPlatforms qingma.QmPlatforms
	err := c.ShouldBindJSON(&qmPlatforms)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	grpcClient := operate.NewOperateServiceClient(global.OperateGrpc)
	_, err = grpcClient.UpdatePlatform(context.Background(), &operate.UpdatePlatformRequest{
		Platform: &operate.Platform{
			Id:         int64(qmPlatforms.ID),
			GameHash:   qmPlatforms.GameHash,
			PlatformId: qmPlatforms.PlatformId,
			Appid:      qmPlatforms.Appid,
			Secret:     qmPlatforms.Secret,
			Cookie:     qmPlatforms.Cookie,
			Divide:     qmPlatforms.Divide,
			OpenidFlag: qmPlatforms.OpenidFlag,
			CookieTwo:  qmPlatforms.CookieTwo,
		},
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithMessage("更新成功", c)
}

// FindQmPlatforms 用id查询qmPlatforms表
// @Tags QmPlatforms
// @Summary 用id查询qmPlatforms表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query qingma.QmPlatforms true "用id查询qmPlatforms表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /qmPlatforms/findQmPlatforms [get]
func (qmPlatformsApi *QmPlatformsApi) FindQmPlatforms(c *gin.Context) {
	ID := c.Query("ID")

	//转成int64
	id := cast.ToInt64(ID)

	grpcClient := operate.NewOperateServiceClient(global.OperateGrpc)
	platform, err := grpcClient.GetPlatform(context.Background(), &operate.GetPlatformRequest{
		Id: id,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithData(platform, c)
}

// GetQmPlatformsList 分页获取qmPlatforms表列表
// @Tags QmPlatforms
// @Summary 分页获取qmPlatforms表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query qingmaReq.QmPlatformsSearch true "分页获取qmPlatforms表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /qmPlatforms/getQmPlatformsList [get]
func (qmPlatformsApi *QmPlatformsApi) GetQmPlatformsList(c *gin.Context) {
	var pageInfo qingmaReq.QmPlatformsSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if pageInfo.GameHash == "" {
		pageInfo.GameHash = c.Request.Header.Get("game-hash")
	}

	grpcClient := operate.NewOperateServiceClient(global.OperateGrpc)

	platforms, err := grpcClient.ListPlatform(context.Background(), &operate.ListPlatformRequest{
		Page:     int64(pageInfo.Page),
		PageSize: int64(pageInfo.PageSize),
		GameHash: pageInfo.GameHash,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithData(response.PageResult{
		List:     platforms.Platforms,
		Total:    platforms.Total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, c)
}
